﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{
    public class AjaxController : Controller 
    {
        //[HttpPost]
        //public ActionResult Login(JinianNet.JNCms.Models.Users account)
        //{
        //    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, account.UserName, DateTime.Now, DateTime.Now.AddDays(1), false, "admin");
        //    base.ControllerContext.HttpContext.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)));
        //    return Json(new { Message="登录成功",Success=true });
        //}

        //[HttpPost]
        //public ActionResult Register(JinianNet.JNCms.Models.Users user)
        //{
        //    JinianNet.JNCms.Data.IUsers dal = JinianNet.JNCms.Data.DataAccess.CreateUsers();
        //    if(string.IsNullOrEmpty(user.Account))
        //        return Json(new { Message = "帐号不能为空", Success = false });
        //    if(user.Account.Length> int.Parse(Config.Site.UserNameLengthMax) || user.Account.Length< int.Parse(Config.Site.UserNameLengthMin))
        //        return Json(new { Message = string.Concat("用户名只能为",Config.Site.UserNameLengthMin," - ",Config.Site.UserNameLengthMax," 位"), Success = false });
            
        //    return Json(new { Message = "注册成功", Success = true });
        //}

        public ActionResult Region(string item)
        {
            return Json(JinianNet.JNCms.Data.DataAccess.CreateRegion().GetList(item), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult LoginMessage()
        {
            Hashtable hash = new Hashtable();
            Users user = null;
            HttpCookie cookie = Request.Cookies["User"];
            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookie.Value);
                if (authTicket != null)
                {
                    if (Session["User"] != null)
                    {
                        user = (Users)Session["User"];
                    }
                    if (user == null)
                    {
                        user = Data.DataAccess.CreateUsers().GetItem(authTicket.UserData);
                    }

                    if (user != null)
                    {
                        hash["status"] = true;
                        hash["name"] = user.NickName;
                        hash["admin"] = user.IsAdministrator;
                        hash["id"] = user.ID;
                        hash["sex"] = user.Sex == 0 ? "男" : "女";
                    }
                    else
                    {
                        hash["status"] = false;
                    }


                }
                else
                {
                    hash["status"] = false;
                }
            }
            else
            {
                hash["status"] = false;
            }
            return Json(hash, JsonRequestBehavior.AllowGet);
        }
    }
}
